package com.turing.post.webservice.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.turing.post.datatranfer.util.ActivateCardBusiness;
import com.turing.post.datatranfer.util.ActivateCardDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * 激活卡迁移测试
 * 
 * @author kangzhe
 * 
 */
public class TranferActiveCardTest implements Runnable {

	private String issueCompanyNo;
	private List<String> batchNoList;

	public TranferActiveCardTest() {
	}

	public TranferActiveCardTest(String issueCompanyNo, List<String> batchNoList) {
		this.issueCompanyNo = issueCompanyNo;
		this.batchNoList = batchNoList;
	}

	/**
	 * @param args
	 * @throws ParseException
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		Context ctx = null;
		ActivateCardDao dao = null;
		String name = "datatranfer.impl.ActivateCardDaoImpl" + "#"
				+ ActivateCardDao.class.getName();
		Hashtable env = new Hashtable();
		env.put(InitialContext.INITIAL_CONTEXT_FACTORY,
				"weblogic.jndi.WLInitialContextFactory");
		env.put(InitialContext.PROVIDER_URL, "t3://127.0.0.1:7001");
		try {
			ctx = new InitialContext(env);
		} catch (NamingException e1) {
			e1.printStackTrace();
		}
		try {
			dao = (ActivateCardDao) ctx.lookup(name);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		List<Object[]> list = dao.getBatchNoFromOldActivateCard();
		Map<Integer, ArrayList<String>> batchlist = new HashMap<Integer, ArrayList<String>>();

		int threadcnt = 31;

		for (int i = 0; i < list.size(); i++) {
			Object[] obj = list.get(i);
			Integer thrid = Integer.parseInt(obj[0].toString()) % threadcnt;
			ArrayList<String> o = batchlist.get(thrid);
			if (o == null) {
				o = new ArrayList<String>();
				batchlist.put(thrid, o);
			}
			o.add(obj[0].toString());
		}

		for (Map.Entry<Integer, ArrayList<String>> entry : batchlist.entrySet()) {
			TranferActiveCardTest test = new TranferActiveCardTest("961850",
					entry.getValue());
			Thread thread = new Thread(test);
			thread.start();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void run() {

		// 用户登录成功信息检测
		Context ctx = null;
		// 根据客户端传来的名字查找相应的实现EJB
		ActivateCardBusiness dao = null;
		String name = "datatranfer.impl.ActivateCardBusinessImpl" + "#"
				+ ActivateCardBusiness.class.getName();

		Hashtable env = new Hashtable();
		env.put(InitialContext.INITIAL_CONTEXT_FACTORY,
				"weblogic.jndi.WLInitialContextFactory");
		env.put(InitialContext.PROVIDER_URL, "t3://127.0.0.1:7001");

		try {
			ctx = new InitialContext(env);
		} catch (NamingException e1) {
			e1.printStackTrace();
		}

		try {
			dao = (ActivateCardBusiness) ctx.lookup(name);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		for (String batchNo : batchNoList) {
			System.out.println("批次号:" + batchNo);
			long all = System.currentTimeMillis();
			try {
				dao.tranferActivateCard("961850", batchNo);
			} catch (PCPSApplicationException e) {
				e.printStackTrace();
			}
			System.out.println("批次号:" + batchNo + "耗时："
					+ (System.currentTimeMillis() - all) / 1000);
		}
	}

}
